package com.starblink.search.category.ui.main.cell

import android.widget.ImageView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.ext.load
import com.starblink.basic.route.RoutePage
import com.starblink.rocketreserver.fragment.CategoryF
import com.starblink.search.category.data.MainPlaceThreeModel
import com.starblink.search.databinding.CellCategoryThreeSpanBinding
import com.starblink.search.databinding.CellCategoryThreeSpanItemBinding

/**
 * tab 三级类目显示
 */
class ThreeLevelSpanCell(private val oneLevelId: String, private val categoryName:String, binding: CellCategoryThreeSpanBinding) :
    BaseBindingVH<MainPlaceThreeModel, CellCategoryThreeSpanBinding>(binding) {

    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
        binding.vAll.tvName.text = string(com.starblink.basic.style.R.string.view_all)
        binding.vAll.img.enableCorner(1000)
        binding.vAll.img.setImageResource(com.starblink.basic.style.R.drawable.ic_dot_more_cate)

        binding.vAll.root.trackData(
            element = SpmElementDef.Element_33050,
            page = SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
        )
        binding.vAll.root.setOnClickListener {
            navigationTo(RoutePage.Search.PAGE_GOODS_CATEGORY) {
                extraOf(RoutePage.ARG.ID to oneLevelId)
            }
        }
    }


    override fun onBind(model: MainPlaceThreeModel, position: Int) {
        if (model.loading || model.categories.isNullOrEmpty()) {
            binding.groupLoading.visible()
            binding.vContent1.root.gone()
            binding.vContent2.root.gone()
            binding.vContent3.root.gone()
            binding.vContent4.root.gone()
            binding.vAll.root.gone()
        } else {
            binding.groupLoading.gone()

            binding.vAll.root.visible()
            model.categories?.run {
                binding.vContent1.setItem(if (size > 0) get(0) else null)
                binding.vContent2.setItem(if (size > 1) get(1) else null)
                binding.vContent3.setItem(if (size > 2) get(2) else null)
                binding.vContent4.setItem(if (size > 3) get(3) else null)
            }
        }
    }

    private fun CellCategoryThreeSpanItemBinding.setItem(item: CategoryF?) {
        root.visibleOrGone(item != null) {
            trackData(
                element = SpmElementDef.Element_33049,
                page = SpmPageDef.CATEGORY_MAIN_GOODS_PAGE,
                params = mapOf(
                    "thirdClassName" to (item!!.name ?: ""),
                    "tabName" to (categoryName ?: ""),
                ),
            )
            img.enableCorner(1000)
            tvName.text = item!!.name
            img.load(url = item.imageUrl)
            img.scaleType = ImageView.ScaleType.CENTER_CROP
            tvDiscount.visibleOrGone(!item.highestDiscountInfo.isNullOrEmpty()) {
                text = "Up to ${item.highestDiscountInfo}"
            }
            setOnClickListener {
                navigationTo(RoutePage.Search.PAGE_CATEGORY_GOODS) {
                    extraOf(
                        RoutePage.ARG.ID to (item.id?.toString() ?: ""),
                        RoutePage.ARG.TITLE to (item.name ?: ""),
                        RoutePage.ARG.IS_SALE_ZONE to true
                    )
                }
            }
        }

    }

}